package com.example.fw;

import static com.example.fw.RandomizeHelper.getRandomNumber;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase
{
	private SortedListOf<ContactData> cashedContacts;
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager)
	{
		super(manager);
	}

	public SortedListOf<ContactData> getUIContacts()
	{
		if (cashedContacts == null)
		{
			rebuildCache();
		}
		return cashedContacts;
	}

	public ContactHelper createContact(ContactData contact)
	{
		manager.navigateTo().mainPage();
		openNewContactPage();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		// rebuildCache();
		manager.getModel().addContact(contact);
		return this;
	}

	public ContactHelper modifyContact(ContactData contact, int index)
	{
		manager.navigateTo().mainPage();
		initContactModificationByIndex(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		// rebuildCache();
		manager.getModel().removeContact(index).addContact(contact);
		return this;
	}

	public ContactHelper deleteContact(int contactNumber)
	{
		manager.navigateTo().mainPage();
		initContactModificationByIndex(contactNumber);
		submitContactDeletion();
		returnToHomePage();
		// rebuildCache();
		manager.getModel().removeContact(contactNumber);
		return this;
	}

	// ----------------------------------------------->

	public void rebuildCache()
	{
		cashedContacts = new SortedListOf<ContactData>();
		List<WebElement> lines = driver.findElements(By.name("entry"));
		for (WebElement line : lines)
		{
			List<WebElement> cells = line.findElements(By.tagName("td"));

			ContactData contact = new ContactData()
					.withLastname(cells.get(1).getText())
					.withFirstname(cells.get(2).getText())
					.withEmail1(cells.get(3).getText())
					.withPhoneHome(cells.get(4).getText());

			cashedContacts.add(contact);
		}
	}

	public ContactHelper openNewContactPage()
	{
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType)
	{
		/* main info group */
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getPhoneHome());
		type(By.name("mobile"), contact.getPhoneMobile());
		type(By.name("work"), contact.getPhoneWork());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
		/* birth date group */
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		/* secondary info group */
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		if (formType == CREATION)
		{
			selectGroup(contact);
		}
		return this;
	}

	private ContactHelper selectGroup(ContactData contact)
	{
		if (contact.getGroup().isEmpty())
		{
			selectByIndex(By.name("new_group"),
					getRandomNumber(1, driver.findElements(By.xpath(".//select[@name = 'new_group']/option")).size() - 1));
		}
		else
		{
			selectByText(By.name("new_group"), contact.getGroup());
		}
		return this;
	}

	public ContactHelper returnToHomePage()
	{
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModificationByIndex(int contactNumber)
	{
		click(By.xpath("(.//*[@id='maintable']//img[@title='Edit']/parent::a)[" + (contactNumber + 1) + "]"));
		return this;
	}

	public ContactHelper submitContactCreation()
	{
		click(By.name("submit"));
		cashedContacts = null;
		return this;
	}

	public ContactHelper submitContactModification()
	{
		click(By.xpath(".//*[@value='Update']"));
		cashedContacts = null;
		return this;
	}

	private ContactHelper submitContactDeletion()
	{
		click(By.xpath(".//*[@value='Delete']"));
		cashedContacts = null;
		return this;
	}
}
